package waal.pc.backend;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;


import waal.pc.Waal;

public class Log {

	public static int debuglevel = Waal.debug;
	private static StyledDocument doc = new DefaultStyledDocument();
	private static SimpleDateFormat sdf = new SimpleDateFormat("[yy-MM-dd|HH:mm:ss] -- ");
	private static Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
	private static Style regular;
	private static Style debug;
	private static Style date;
	private static Style error;
	private static Style corty;
	
	static {
		regular = Log.doc.addStyle("regular", def);
		StyleConstants.setFontFamily(Log.def, "SansSerif");
		Log.debug = Log.doc.addStyle("debug", regular);
        StyleConstants.setItalic(Log.debug, true);
        StyleConstants.setForeground(Log.debug, Color.gray);
        Log.date = Log.doc.addStyle("date", regular);
        StyleConstants.setForeground(Log.date, Color.DARK_GRAY);
        StyleConstants.setBold(Log.date, true);
        Log.error = Log.doc.addStyle("error", regular);
        StyleConstants.setForeground(Log.error, Color.RED);
        StyleConstants.setBold(Log.error, true);
        Log.corty = Log.doc.addStyle("corty", regular);
        StyleConstants.setForeground(Log.corty, Color.GREEN);
        StyleConstants.setBold(Log.corty, false);
	}
	
	private static void addToLog(String text, String styleName) {
		try {
			Log.doc.insertString(0, text + "\n", Log.doc.getStyle(styleName));
			Log.doc.insertString(0, Log.sdf.format(new Date()), Log.doc.getStyle("date"));
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void addToLog(String text) {
		Log.addToLog(text,"regular");
		
	}
	
	public static void addToErrorLog(String text) {
		Log.addToLog(text,"regular");
		System.err.println(text);
		
	}
	
	public static void addToDebug(int level, String className, String methodeName, String text) {
		if(level <= Log.debuglevel) {
			String debugText = "[" + level + "][" + className + "]{" + methodeName + "} -- " + text;
			Log.addToLog(debugText, "debug");
			System.out.println(debugText);
		}
	}
	
	public static void addToCortyDebug(int number, String text) {
		String debugText = "[C][Corty]{" + number + "} -- " + text;
		Log.addToLog(debugText, "corty");
		System.out.println(debugText);

	}
	
	public static StyledDocument getLogDoc() {
		return Log.doc;
	}
}
